﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web.UI.HtmlControls;

namespace Chaks.SharePoint.jQuery
{
    public class AccordionToolPart : ToolPart
    {
        readonly string[] themes=new[]{"smoothness","darkness"};
        
        protected Label lblAnnouncementsList=new Label();
        protected DropDownList ddlAnnouncementsList=new DropDownList();

        protected Label lblTheme=new Label();
        protected DropDownList ddlThemesList=new DropDownList();

        /// <summary>
        /// Initializes a new instance of the <see cref="JQueryToolPart"/> class.
        /// </summary>
        public AccordionToolPart()
        {
            Title = "jQuery Web Parts Options";
        }

        /// <summary>
        /// Gets the parent web part.
        /// </summary>
        /// <value>The parent web part.</value>
        protected AccordionWebPart ParentWebPart
        {
            get
            {
                return (AccordionWebPart)ParentToolPane.SelectedWebPart;
            }
        }

        /// <summary>
        /// Sends the tool part content to the specified HtmlTextWriter object, which writes the content to be rendered on the client.
        /// </summary>
        /// <param name="output">The HtmlTextWriter object that receives the tool part content.</param>
        protected override void RenderToolPart(HtmlTextWriter output)
        {
            AddAnnouncementsControls(ref output);
        }
       

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            #region Announcements List

            Controls.Add(lblAnnouncementsList);
            Controls.Add(ddlAnnouncementsList);

            #endregion

            #region Theme

            //Controls.Add(lblTheme);
            //Controls.Add(ddlThemesList);

            #endregion

        }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        /// <exception cref="T:System.InvalidOperationException">
        /// The <see cref="T:System.Web.UI.WebControls.WebParts.EditorZoneBase"/> that contains the <see cref="T:System.Web.UI.WebControls.WebParts.EditorPart"/> control is null.
        /// </exception>
        protected override void OnPreRender(EventArgs e)
        {
            SPSite site = SPContext.GetContext(HttpContext.Current).Site;

            using (SPWeb web = site.OpenWeb())
            {

                ddlThemesList.Items.Clear();
                ddlAnnouncementsList.Items.Clear();

                foreach (SPList list in web.Lists)
                {
                    if (list.BaseTemplate == SPListTemplateType.Announcements)
                    {
                        string listTitle = list.Title;
                        string listID = list.ID.ToString();

                        ListItem annItem = new ListItem(listTitle, listID);
                        annItem.Selected = listID == ParentWebPart.LibraryName;

                        ddlAnnouncementsList.Items.Add(annItem);
                    }
                }
            }

            //foreach (string theme in themes)
            //{
            //    ListItem itemTheme = new ListItem(theme, theme);
            //    itemTheme.Selected = theme == ParentWebPart.Theme;
            //    ddlThemesList.Items.Add(itemTheme);
            //}

            base.OnPreRender(e);
        }

        /// <summary>
        /// Called when the user clicks the OK or the Apply button in the tool pane.
        /// </summary>
        public override void ApplyChanges()
        {
            //ParentWebPart.Theme = ddlThemesList.SelectedValue;
            ParentWebPart.LibraryName = ddlAnnouncementsList.SelectedValue;

            base.ApplyChanges();
        }

        /// <summary>
        /// Adds the theme controls.
        /// </summary>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        private HtmlTextWriter AddThemeControls(ref HtmlTextWriter output)
        {
            #region Theme

            output.Write("<br/>");
            output.Write("<div>");

            lblTheme.Text = "Theme";
            lblTheme.RenderControl(output);

            output.Write("<br/>");
            ddlThemesList.RenderControl(output);

            output.Write("</div>");

            #endregion

            return output;
        }

        /// <summary>
        /// Adds the announcements controls.
        /// </summary>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        private HtmlTextWriter AddAnnouncementsControls(ref HtmlTextWriter output)
        {
            #region Announcements List

            output.Write("<br/>");
            output.Write("<div>");

            lblAnnouncementsList.Text = "Announcements List";
            lblAnnouncementsList.RenderControl(output);

            output.Write("<br/>");
            ddlAnnouncementsList.RenderControl(output);

            output.Write("</div>");

            #endregion

            return output;
        }
    }
}
